package com.example.happiness.service.admin.Impl;

import com.example.happiness.mapper.ChatMapper;
import com.example.happiness.mapper.UserMapper;
import com.example.happiness.pojo.Chat;
import com.example.happiness.pojo.User;
import com.example.happiness.pojo.adminvo.AdminChatVO;
import com.example.happiness.service.admin.AdminChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminChatServiceImpl implements AdminChatService {
    @Autowired
    ChatMapper chatMapper;
    @Autowired
    UserMapper userMapper;

    @Transactional
    public List<AdminChatVO> getChatList() {
        List<String> useridlist = chatMapper.getUserList();
        List<AdminChatVO> adminChatVOList = new ArrayList<>();
        for (String userid : useridlist) {
            AdminChatVO adminChatVO = new AdminChatVO();
            User user = userMapper.selectByPrimaryKey(userid);
            List<Chat> chatList = chatMapper.getChatList(userid);
            adminChatVO.setUserId(user.getUserid());
            adminChatVO.setUsername(user.getRealname());
            adminChatVO.setMessages(chatList);
            adminChatVOList.add(adminChatVO);
        }
        System.out.println(adminChatVOList);
        return adminChatVOList;
    }
}
